package cn.lg.soar.system.auth.client;

import cn.lg.soar.common.util.BeanCopier;
import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.AuthRole;
import cn.lg.soar.system.api.model.RoleDTO;
import cn.lg.soar.system.api.service.IRoleApi;
import cn.lg.soar.system.auth.entity.Role;
import cn.lg.soar.system.auth.service.*;
import cn.lg.soar.system.config.AuthCacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiIgnore
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/role")
public class RoleApiClient implements IRoleApi {

    @Autowired
    private IRoleService service;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private OrgRoleService orgRoleService;
    @Autowired
    private PostRoleService postRoleService;
    @Autowired
    private IUserService userService;

    private final static BeanCopier<Role, RoleDTO> ENTITY_TO_DTO = BeanCopier.create(Role.class, RoleDTO.class);

    @GetMapping("idsByUserId")
    @Override
    public List<Long> idsByUserId(@RequestParam Long userId) {
        return userRoleService.getRoleIds(userId);
    }

    @GetMapping("idsByOrgId")
    @Override
    public List<Long> idsByOrgId(@RequestParam Long orgId) {
        return orgRoleService.getRoleIds(orgId);
    }

    @GetMapping("idsByPostId")
    @Override
    public List<Long> idsByPostId(@RequestParam Long postId) {
        return postRoleService.getRoleIds(postId);
    }

    @GetMapping("listByCodes")
    @Override
    public List<RoleDTO> listByCodes(@RequestParam Collection<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return ENTITY_TO_DTO.convertBatch(service.listByCodes(codes))
                .collect(Collectors.toList());
    }

    @GetMapping("listByIds")
    @Override
    public List<RoleDTO> listByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return ENTITY_TO_DTO.convertBatch(service.listByIds(ids))
                .collect(Collectors.toList());
    }

    @GetMapping("getById")
    @Override
    public RoleDTO getById(@RequestParam Long id) {
        return ENTITY_TO_DTO.convertNullable(service.getById(id));
    }

    @GetMapping("codesByIds")
    @Override
    public List<String> codesByIds(@RequestParam List<Long> roleIds) {
        return service.codesByIds(roleIds);
    }

    @GetMapping("userRoleList")
    @Override
    public List<AuthRole> userRoleList(Long userId) {
        List<Long> roleIds = userService.getUserCurrentAllRoleIds(userId);
        List<Role> roles = service.listByIds(roleIds, AuthCacheKey.ROLE_BY_ID);
        return roles.stream().map(x -> {
            AuthRole authRole = new AuthRole();
            authRole.setId(x.getId());
            authRole.setCode(x.getCode());
            authRole.setName(x.getName());
            authRole.setAlias(x.getAlias());
            authRole.setRemark(x.getRemark());
            return authRole;
        }).collect(Collectors.toList());
    }
}
